package com.example.administrator.capital.login;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.capital.MainActivity;
import com.example.administrator.capital.R;
import com.example.administrator.capital.base.BaseActivity;
import com.example.administrator.capital.presenter.LoginPresenter;
import com.example.administrator.capital.presenter.base.IViewRequest;
import com.example.administrator.capital.utils.CommonUtils;
import com.example.administrator.capital.utils.ToastUtil;
import com.example.administrator.capital.widget.CountDownTimerUtils;
import com.lzy.okgo.model.Response;

import butterknife.BindView;
import butterknife.ButterKnife;

/*登录界面*/
public class FastLoginActivity extends BaseActivity {
    @BindView(R.id.phone)
    EditText mPhone;
    @BindView(R.id.code)
    EditText mCode;
    @BindView(R.id.get_code)
    TextView mGetCode;
    @BindView(R.id.login)
    Button mLogin;
    @BindView(R.id.register)
    TextView mRegister;
    private String phone;
    private TextView mBack;
    private LoginPresenter mLoginPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_fast_login;
    }
    @Override
    public void initView() {
        mBack = findViewById(R.id.app_title_back);
        mLoginPresenter = new LoginPresenter(null);

    }
    @Override
    public void setListener() {
        mGetCode.setOnClickListener(this);
        mLogin.setOnClickListener(this);
        mRegister.setOnClickListener(this);
        mBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }
    @Override
    public void setData() {
    }

    private void getYZM( String mobile) {
        mLoginPresenter.getYZM(new IViewRequest<String>() {
            @Override
            public void onFailMsg(String var) {
                ToastUtil.showShort(var);
            }

            @Override
            public void onCode(int code) {
                ToastUtil.showShort(code);

            }

            @Override
            public void onSuccess(Response<String> data) {
                CountDownTimerUtils utils = new CountDownTimerUtils(mGetCode, 60000, 1000);
                utils.start();
            }

            @Override
            public void onFail(Response<String> error) {

            }
        },mobile);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
    private boolean isPhoneNumber() {
        phone = mPhone.getText().toString().trim();
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(mContext, "手机号不能为空!", Toast.LENGTH_SHORT).show();
            return false;
        } else if (CommonUtils.isMobileNO(phone)) {
            return true;
        } else {
            Toast.makeText(mContext, "手机号不合法!", Toast.LENGTH_SHORT).show();
            return false;
        }
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.get_code:
                if (isPhoneNumber()) {
                    getYZM(phone);
                }
                break;
            case R.id.login:
                showActivity(MainActivity.class,null);
                finish();
                break;
            case R.id.register:
                showActivity(RegisterActivity.class, null);
                finish();
                break;
        }
    }
}
